(()=> {
	let ele = {};
	ele.fubu = $('#fabu');
	ele.listBox = $('#listBox');
	ele.more = $('#more');
	ele.empty = $('#empty');
	
	
	ele.fubu.onclick = ()=> {
		location.href = '../page/releaseSell.html'
	}
	
	let page = 1;
	
	houseList(page);
	// 我的求购/出售列表  1 求购 2出售
	function houseList(page) {
		ele.more.innerText = '加载中...';
		let token = getCookie("token");
		ajax({
			url: API_PATH.HOUSE_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				type: 2,
				page:page
			},
			success: res => {
				if (res.code == 1) {
					let detail = res.data.list.data;
					if (detail.length === 0) {
						page = 0;
						ele.more.style.display = "none";
						ele.empty.style.display = "block";
					} else {
						drawList(detail);
						page++;
						if (page > res.data.list.last_page) {
							ele.more.innerText = '没有更多了';
							ele.more.onclick = null;
						}
						else {
							ele.more.innerText = '加载更多';
							ele.more.onclick = () => houseList(page);
						}
					}
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	function drawList(_data) {
		let html = '';
		let pj_type = '';
		for(let i = 0; i < _data.length; i++) {
			let imgsrc = _data[i].class.logo == null ||  _data[i].class.logo == '' ? 'http://img.qxgs.cc/tiantianlaishouzu/img/luck/thingbg.png' : IMG_DOMAIN + _data[i].class.logo
			pj_type = _data[i].class.hammer == 6 ? 'itemred' :  _data[i].class.hammer == 2 ? 'itemgreen' : _data[i].class.hammer == 3 ? 'itemblue' :  _data[i].class.hammer == 4 ? 'itempurple' :  _data[i].class.hammer == 5 ? 'itemorange' : '';
			let iscan = _data[i].is_can == 1 ? '<p class="iscan">（不在可交易状态）</p>' : '';
			html += '<div class="list">'
						+'<div class="left">'
							+'<div class="leftimg '+pj_type+'">'
								+'<img src="'+imgsrc+'">'
							+'</div>'
							+'<div class="leftin">'
								+'<p class="mobile">'+_data[i].class.name+'</p>'
								+iscan
								+'<div class="">'
									+'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/home/jinbi.png" alt="">'
									+'<span>'+_data[i].money+'/个</span>'
								+'</div>'
							+'</div>'
						+'</div>'
						+'<div class="center">'
							+'<p class="num">出售数量:<span>'+_data[i].lose_num+'</span></p>'
							+'<p class="time" data="'+_data[i].over_time+'">剩余时间：</p>'
						+'</div>'
						+'<button class="btns" data="'+_data[i].id+'">下架</button>'
					+'</div>'
		}
		ele.listBox.innerHTML = html;
		
		
		// 下架
		let btns = $('.btns');
		for(let i = 0; i < btns.length; i++) {
			btns[i].onclick = ()=> {
				// btns[i].getAttribute('data');
				houseCancel(btns[i].getAttribute('data'))
			}
		}
		
		let times = $('.time');
		for(let i = 0; i < times.length; i++) {
			setInterval(()=> {
				times[i].innerHTML = '剩余时间：' + getCountTime(times[i].getAttribute('data'))
				// console.log(getCountTime(times[0].getAttribute('data')));
			},1000)
			
			// btns[i].getAttribute('data');
			// houseCancel(btns[i].getAttribute('data'))
		}
		
	}
	
	function houseCancel(id) {
		// console.log(id,num,money,name);
		let token = getCookie("token");
		ajax({
			url: API_PATH.HOUSE_CANCEL,
			method: 'POST',
			header: {
				'Authorization': token,
			},
			data: {
				id: id
			},
			success: res => {
				if (res.code == 1) {
					openTips(res.msg);
					ele.listBox.innerHTML = '';
					houseList(page);
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	
	
	
})()